package com.syezon.wifikey.pwd.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.syezon.wifikey.app.WiFiKeyApp;
import com.syezon.wifikey.home.info.WifiListInfo;
import com.syezon.wifikey.wifi.WiFiObserver;
import com.syezon.wifikey.wifi.WifiListUtils;
import com.syezon.wifikey.wifi.info.WifiInfo;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2018/9/13.
 */

public class AddPwdViewModel extends AndroidViewModel {

    private MutableLiveData<List<WifiInfo>> wifiListLiveData = new MutableLiveData<>();

    private List<WifiInfo> mWifiList = new ArrayList<>();

    private WiFiObserver wiFiObserver = new WiFiObserver() {
        @Override
        public void onWifiStateChange(boolean isOpen) {

        }

        @Override
        public void onGpsStateChange(boolean isGpsOpen) {

        }

        @Override
        public void onWiFiListChange(List<WifiInfo> wifiInfos) {
            if (wifiInfos.size() > 0 && mWifiList.size() == 0) {
                List<WifiInfo> wifiBeansExclude = getWifiBeansExclude(wifiInfos);
                mWifiList.addAll(wifiBeansExclude);
                wifiListLiveData.setValue(wifiBeansExclude);
            }
        }

        @Override
        public void onWiFiListInfoChange(List<WifiListInfo> wifiListInfos) {

        }

        @Override
        public void onLocationPermissionDeny() {

        }

        @Override
        public void onGpsPermissionDeny() {

        }
    };

    public AddPwdViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<List<WifiInfo>> getWifiListLiveData() {
        return wifiListLiveData;
    }

    public void getList() {
        WifiListUtils.getInstance().registerWifiObserver(wiFiObserver);
    }

    private List<WifiInfo> getWifiBeansExclude(List<WifiInfo> wifiInfos) {
        List<SaveWifiPwdInfo> all = WiFiKeyApp.getDatabase().getWifiPwdDao().getAll();
        List<WifiInfo> result = new ArrayList<>();
        for (int i = 0; i < wifiInfos.size(); i++) {
            WifiInfo wifiInfo = wifiInfos.get(i);
            boolean isSaved = false;
            if (all != null && all.size() > 0) {
                Iterator<SaveWifiPwdInfo> iterator = all.iterator();
                while (iterator.hasNext()) {
                    SaveWifiPwdInfo next = iterator.next();
                    if (wifiInfo.getSsid().equals(next.getSsid())) {
                        isSaved = true;
                    }
                }
            }
            if (!isSaved) {
                result.add(wifiInfo);
            }
        }
        return result;
    }

    @Override
    protected void onCleared() {
        WifiListUtils.getInstance().removeWifiObserver(wiFiObserver);
        super.onCleared();
    }
}
